package singleCrud

import (
	"GA-SERVER/internal/model/entity"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
)

var (
	User = &TplStruct{
		Label: "用户",
		Name: "User",
		ApiType: "post",
		FileApi: "api_system",
		FileServer:"service_system",
		FileController: "controller_system",
		DataStruct: entity.SysUser{},
	}
	Role = &TplStruct{
		Label: "角色",
		Name: "Role",
		ApiType: "post",
		FileApi: "api_system",
		FileServer:"service_system",
		FileController: "controller_system",
		DataStruct: entity.SysRole{},
	}
	KaiPiao = &TplStruct{
		Label: "开票测试",
		Name: "KaiPiao",
		ApiType: "post",
		FileApi: "api_system",
		FileServer:"service_system",
		FileController: "controller_system",
		DataStruct: entity.KAIPIAO{},
	}
)
var CrudObj = []*TplStruct{
	User,
	Role,
	KaiPiao,
}

func List()[]map[string]string{
	var arr []map[string]string
	for _, val := range CrudObj {
		arr = append(arr, map[string]string{
			"name":val.Label,
			"value":val.Name,
		})
	}
	return arr
}
func getData(value string)(res map[string]interface{},err error){
	var aa *TplStruct
	err1 := 1
	for _, val := range CrudObj {
		if val.Name == value {
			aa = val
			err1 = 0
		}
	}
	if err1 == 1 {
		err = gerror.New("未查询到结构体")
		return nil,err
	}
	fields,name,num:=AnalyseStruct(aa.DataStruct)
	aa.EntityName = name
	aa.NumField = num
	aa.Fields =  fields
	var mapFields []map[string]interface{}
	for _, val := range aa.Fields {
		mapFields = append(mapFields, g.Map{
			"Index":       val.Index,
			"Name":        val.Name,
			"JsonName":    val.JsonName,
			"Description": val.Description,
		})
	}
	params := g.Map{
		"Label":          aa.Label,
		"Name":           aa.Name,
		"ApiType":        aa.ApiType,
		"FileApi":        aa.FileApi,
		"FileServer":     aa.FileServer,
		"FileController": aa.FileController,
		"EntityName":     aa.EntityName,
		"NumField":       aa.NumField,
		"Fields":         mapFields,
	}
	return params,err
}
